using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace GoogleNet
{

    /// <summary>
    /// This is an abstract element and cannot be used directly in a KML file. 
    /// It provides elements for specifying the color and color mode of extended style types. 
    /// </summary>
    public abstract class KmlColorStyle : IKmlElement
    {
        private string tagName = "ColorStyle";

        #region Color
        private Color color;

        /// <summary>
        /// Color values are expressed in hexadecimal notation, including opacity (alpha) values. 
        /// The order of expression is alpha, blue, green, red (<I>aabbggrr</I>). 
        /// The range of values for any one color is 0 to 255 (00 to ff). 
        /// For opacity, 00 is fully transparent and ff is fully opaque. 
        /// For example, if you want to apply a blue color with 50 percent opacity to an overlay, 
        /// you would specify the following: &lt;color&gt;7fff0000&lt;/color&gt; 
        /// </summary>
        /// <value>The color.</value>
        public Color Color
        {
            get { return color; }
            set { color = value; }
        }
        #endregion

        #region ColorMode
        private KmlColorModes colorMode = KmlColorModes.normal;

        /// <summary>
        /// Gets or sets the color rendition mode for the associated color tag.
        /// </summary>
        /// <value>The color mode.</value>
        public KmlColorModes ColorMode
        {
            get { return colorMode; }
            set { colorMode = value; }
        }
        #endregion

        #region IKmlElement Members

        /// <summary>
        /// Name of the KML tag for this specific element.
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        public virtual string TagName
        {
            get { return tagName; }
        }

        #endregion

        #region IKmlGenerator Members

        /// <summary>
        /// Generates the KML for this element including its child elements.
        /// </summary>
        /// <param name="writer">The XML writer that will be used to generate the KML.</param>
        public virtual void GenerateKml(System.Xml.XmlTextWriter writer)
        {
            //writer.WriteStartElement(tagName);

            if (color != null)
                writer.WriteElementString("color", convertColorToKmlColor(color));

            if (colorMode != KmlColorModes.normal)
                writer.WriteElementString("colorMode", colorMode.ToString());

            //writer.WriteEndElement();
        }

        #endregion

        /// <summary>
        /// Converts the .NET color to its KML's hexadecimal representation.
        /// </summary>
        /// <param name="c">The color to convert</param>
        /// <returns></returns>
        private string convertColorToKmlColor(Color c)
        {
            string hexColor = string.Format("{0:x2}", c.A) +
                                string.Format("{0:x2}", c.B) +
                                string.Format("{0:x2}", c.G) +
                                string.Format("{0:x2}", c.R);

            return hexColor;
        }
    }
}
